using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class LiftItem : BaseItem
	{
		public enum State
		{
			Height0 = 0,
			Height1 = 1,
			Height2 = 2
		}

		public enum Anim
		{
			Static = 0,
			MoveToHeight1 = 1,
			MoveToHeight2 = 2,
			MoveToHeight0 = 3
		}

		private float HEIGHT_0;

		private float HEIGHT_1 = 0.3f;

		private float HEIGHT_2 = 0.6f;

		private float SCALE_0 = 1f;

		private float SCALE_1 = 1.1f;

		private float SCALE_2 = 1.2f;

		private Transform lift;

		private Transform spring1;

		private Transform spring2;

		private float curY;

		private float curScale;

		private Anim anim;

		private State state;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			lift = model.Find("Lift");
			lift.SetLocalZ(1E-05f);
			spring1 = model.Find("Spring1");
			spring1.SetLocalZ(2E-05f);
			spring2 = model.Find("Spring2");
			spring2.SetLocalZ(2E-05f);
			state = (State)data.state;
			if (state == State.Height1)
			{
				SetLiftY(HEIGHT_1);
				SetSpringScaleY(SCALE_1);
			}
			else if (state == State.Height2)
			{
				SetLiftY(HEIGHT_2);
				SetSpringScaleY(SCALE_2);
			}
		}

		private void SetSpringScaleY(float val)
		{
			spring1.SetLocalScaleY(val);
			spring2.SetLocalScaleY(val);
		}

		private void SetLiftY(float y)
		{
			lift.SetLocalY(y);
			if (platforms != null)
			{
				platforms[0].SetLocalY(y);
			}
		}

		protected override void LateUpdate()
		{
			if (anim == Anim.Static)
			{
				return;
			}
			if (anim == Anim.MoveToHeight0)
			{
				curY -= Time.deltaTime;
				if (curY <= HEIGHT_0)
				{
					curY = HEIGHT_0;
					state = State.Height0;
					anim = Anim.Static;
				}
			}
			else if (anim == Anim.MoveToHeight1)
			{
				curY += Time.deltaTime;
				if (curY >= HEIGHT_1)
				{
					curY = HEIGHT_1;
					state = State.Height1;
					anim = Anim.Static;
				}
			}
			else if (anim == Anim.MoveToHeight2)
			{
				curY += Time.deltaTime;
				if (curY >= HEIGHT_2)
				{
					curY = HEIGHT_2;
					state = State.Height2;
					anim = Anim.Static;
				}
			}
			SetLiftY(curY);
			curScale = 1f + curY / 3f;
			SetSpringScaleY(curScale);
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim == Anim.Static)
			{
				if (state == State.Height0)
				{
					anim = Anim.MoveToHeight1;
					curY = HEIGHT_0;
					curScale = SCALE_0;
					Singleton<AudioManager>.Instance.PlaySound("sound_car_lift");
				}
				else if (state == State.Height1)
				{
					anim = Anim.MoveToHeight2;
					curY = HEIGHT_1;
					curScale = SCALE_1;
					Singleton<AudioManager>.Instance.PlaySound("sound_car_lift");
				}
				else
				{
					anim = Anim.MoveToHeight0;
					curY = HEIGHT_2;
					curScale = SCALE_2;
					Singleton<AudioManager>.Instance.PlaySound("sound_car_lift");
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (state != 0)
			{
				obj["33"] = (int)state;
			}
		}
	}
}
